﻿#region

using System;
using System.Collections.Generic;
using Microsoft.Win32;

#endregion

namespace MediaSage.Lib.Utilities
{
    public static class RegistryHelper
    {
        #region Methods

        public static bool DeleteKey(string KeyName)
        {
            try
            {
                if (KeyExists(KeyName))
                {
                    Registry.LocalMachine.DeleteSubKeyTree(KeyName);
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public static object GetKeyValue(string Location, string KeyName)
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Location);
                if (rk == null)
                {
                    rk.Close();
                    return null;
                }
                object returnVal = rk.GetValue(KeyName);
                rk.Close();
                return returnVal;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static IDictionary<string, object> GetKeyValues(string Location)
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Location);
                if (rk == null)
                {
                    return null;
                }

                var returnVal = new Dictionary<string, object>();
                foreach (string valuename in rk.GetValueNames())
                {
                    returnVal.Add(valuename, rk.GetValue(valuename));
                }
                rk.Close();
                return returnVal;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool KeyExists(string KeyName)
        {
            try
            {
                if (Registry.LocalMachine.OpenSubKey(KeyName) == null)
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool SetKeyValue(string Location, string KeyName, object Value)
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Location, true);
                if (rk == null)
                {
                    if (rk == null)
                    {
                        WriteKey(KeyName, Value, true);
                        rk.Close();
                        return true;
                    }
                }
                rk.SetValue(KeyName, Value);
                rk.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool SetKeyValue(string Location, string KeyName, object Value, bool CreateKey)
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(Location);
                if (rk == null)
                {
                    if (CreateKey)
                    {
                        WriteKey(KeyName, Value, true);
                        rk.Close();
                        return true;
                    }
                    return false;
                }
                rk.SetValue(KeyName, Value);
                rk.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool WriteKey(string KeyName, object Value, bool CreateTree)
        {
            try
            {
                string RegPath = string.Empty;
                if (CreateTree)
                {
                    string[] subnodes = KeyName.Split('\\');
                    foreach (string key in subnodes)
                    {
                        if (RegPath != string.Empty)
                        {
                            RegPath = RegPath + @"\";
                        }
                        RegPath = RegPath + key;
                        if (Registry.LocalMachine.OpenSubKey(RegPath) == null)
                        {
                            Registry.LocalMachine.CreateSubKey(RegPath);
                        }
                    }
                }
                RegistryKey rk = Registry.LocalMachine.CreateSubKey(KeyName);
                if (Value != null)
                {
                    rk.SetValue(KeyName.ToUpper(), Value);
                }
                rk.Close();
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        #endregion Methods
    }
}